﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChatDBAccess
{
    public class Search
    {
        public static IQueryable<Channel> SearchChannels(string pattern)
        {
            ChatDataClassesDataContext contex = new ChatDataClassesDataContext();
            var res =
                from ch in contex.Channels
                where ch.Name.Contains(pattern)                
                select ch;

            return res;
        }

        public static IQueryable<ChannelAndSender> MessagesInChannel(int chanId)
        {
            ChatDataClassesDataContext contex = new ChatDataClassesDataContext();
            var res =
                from m in contex.UTCMessages
                where m.ChannelId == chanId
                join s in contex.Users on m.SenderId equals s.UserId
                orderby m.PostDate descending
                select new ChannelAndSender { Message = m, Name = s.UserName };

            return res;
        }

        public static IQueryable<User> SearchUsers(string pattern)
        {
            ChatDataClassesDataContext contex = new ChatDataClassesDataContext();
            var res =
                from u in contex.Users
                where u.UserName.Contains(pattern) && u.UserName != "annonymous"
                select u;

            return res;
        }

        public static IQueryable<Channel> AllChannels()
        {
            return SearchChannels("");
        }

        public static IQueryable<User> AllUsers()
        {
            return SearchUsers("");
        }

        public static IQueryable<MessageAndSender> MessagesBetweenUsers(int senderId, int receiverId)
        {
             ChatDataClassesDataContext contex = new ChatDataClassesDataContext();
             var res =
                 from m in contex.UTUMessages
                 where (m.SenderId == senderId && m.ReceiverId == receiverId) || (m.ReceiverId == senderId && m.SenderId == receiverId)
                 join s in contex.Users on m.SenderId equals s.UserId
                 orderby m.PostDate descending
                 select new MessageAndSender {Message = m, Name = s.UserName};

             return res;
        }

        public static IQueryable<MessageAndSender> MessagesToTheCurrentUser(int userId)
        {
            ChatDataClassesDataContext context = new ChatDataClassesDataContext();
            var res =
                from m in context.UTUMessages
                where m.ReceiverId == userId
                join s in context.Users on m.SenderId equals s.UserId
                orderby m.PostDate descending
                select new MessageAndSender{ Message = m, Name = s.UserName };

            return res;
        }

        public static User FindUserById(int id)
        {
            ChatDataClassesDataContext context = new ChatDataClassesDataContext();
            var res =
                (from u in context.Users
                 where u.UserId == id
                 select u).FirstOrDefault();

            return res;
        }

        public static int FindChannelByName(string name)
        {
            ChatDataClassesDataContext context = new ChatDataClassesDataContext();
            var res =
                (from ch in context.Channels
                 where ch.Name == name
                 select ch).FirstOrDefault();

            if (res == null)
            {
                return -1;
            }
            else
            {
                return res.ChannelId;
            }
        }

        public static int FindUserByName(string name)
        {
            ChatDataClassesDataContext context = new ChatDataClassesDataContext();
            var res =
                (from u in context.Users
                 where u.UserName == name
                 select u).FirstOrDefault();

            if (res == null)
            {
                return -1;
            }
            else
            {
                return res.UserId;
            }
        }

        public static IQueryable<ChannelAndSender> SearchMessagesForAllChannels(string keyWord)
        {
            ChatDataClassesDataContext context = new ChatDataClassesDataContext();
            var res =
                from m in context.UTCMessages
                join u in context.Users on m.SenderId equals u.UserId
                where m.Text.Contains(keyWord)
                select new ChannelAndSender { Message = m, Name = u.UserName};

            return res;
        }

        public static IQueryable<ChannelAndSender> SearchMessagesForChannel(int id, string keyWord)
        {
            ChatDataClassesDataContext context = new ChatDataClassesDataContext();
            var res =
                from m in context.UTCMessages
                join u in context.Users on m.SenderId equals u.UserId
                where m.Text.Contains(keyWord) && m.ChannelId == id
                select new ChannelAndSender { Message = m, Name = u.UserName };

            return res;
        }
    }
}
